<?php

namespace app\common\utils;

use app\admin\model\Ordderdistribution;
use app\admin\model\Order;
use Stripe\StripeClient;

class StripeUtil
{
    CONST STRIPE_KEY = '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';

    protected $stripe;
    public function __construct()
    {
        $this->stripe = new StripeClient(self::STRIPE_KEY);
    }

    /**
     * 创建商品
     */
    public function create_product($name){
        return $this->stripe->products->create([
            'name' => $name,
        ]);
    }

    /**
     * 创建价格
     * @param $product_id
     * @param $order_id
     * @param $type 1:订单 2:配送
     */
    public function create_price($product_id,$money){
        return $this->stripe->prices->create([
            'unit_amount' => $money*100,
            'currency' => 'AUD',
            'tax_behavior' => 'exclusive',
            //'recurring' => ['interval' => 'day'],
            'product' => $product_id,
        ]);
    }

    /**
     * 创建stripe支付
     * 商品订单 https://a962.hzybh.com/web/#/pages/mine/order
     * //配送订单 https://a962.hzybh.com/web/#/pages/mine/podorder
     */
    /**
     * 创建stripe支付
     */
    public function pay($name,$order_id,$type)
    {
        if ($type == 1){
            $order = Order::get($order_id);
        }else{
            $order = Ordderdistribution::get($order_id);
        }
        if ($order->pay_status!=1){
            //订单已支付 返回异常
            return false;
        }
        $pro_res = $this->create_product($name);
        $price_res = $this->create_price($pro_res->id,$order->money);
        if ($type == 1){
            $success_url = 'https://a962.hzybh.com/web/#/pages/mine/order';
        }else {
            $success_url = 'https://a962.hzybh.com/web/#/pages/mine/podorder';
        }
        return $this->stripe->checkout->sessions->create([
            'line_items' => [[
                'price' => $price_res->id,
                'quantity' => 1,
            ]],
            'mode' => 'payment',
            'success_url' => $success_url,
            'cancel_url' => $success_url,
            'locale'=>'en', # 设置页面为英文
            'automatic_tax' => [
                'enabled' => true,
            ],
            'metadata' => [
                'order_id' => $order_id,
                'type' => $type,
            ],
        ]);

    }

    /**
     * 创建stripe支付
     * 商品订单 https://a962.hzybh.com/web/#/pages/mine/order
    配送订单 https://a962.hzybh.com/web/#/pages/mine/podorder
     */
    public function pay2($name,$order_id,$type){
        if ($type == 1){
            $order = Order::get($order_id);
        }else{
            $order = Ordderdistribution::get($order_id);
        }
        if ($order->pay_status!=1){
            //订单已支付 返回异常
            return false;
        }
        $pro_res = $this->create_product($name);
        $price_res = $this->create_price($pro_res->id,$order->money);
        if ($type == 1){
            $success_url = 'https://a962.hzybh.com/web/#/pages/mine/order';
        }else {
            $success_url = 'https://a962.hzybh.com/web/#/pages/mine/podorder';
        }
        return $this->stripe->paymentLinks->create([
            'line_items' => [
                [
                    'price' => $price_res->id,
                    'quantity' => 1,
                ],
            ],
            'after_completion'=>[
                'type'=>'redirect',
                'redirect'=>[
                    'url'=>$success_url
                ]
            ],
            'metadata' => [
                'order_id' => $order_id,
                'type' => $type,
            ],
        ]);
    }
}